<?php
	require_once(realpath(dirname(__FILE__)) . "/PluginsClassiques.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Commande.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Venteprod.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Paysdesc.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Client.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Pays.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Paysdesc.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Modules.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Modulesdesc.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Venteadr.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Produitdesc.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Declinaison.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Declinaisondesc.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Declidisp.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Declidispdesc.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Raison.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Raisondesc.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Mail.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Message.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Messagedesc.class.php");


	class PluginsPaiements extends PluginsClassiques{

		var $defalqcmd = 1;

		function __construct($nom=""){
			parent::__construct($nom);
		}

		/* Compatibilité avec les anciens plugins */
		function PluginsPaiements($nom=""){
			parent::__construct($nom);
		}

		function paiement($commande){


		}

		function getDevise(){

			return $this->modulesdesc->devise;
		}

 		function mail($commande){

			// Les mails sont envoyés en ISO.
			// En effet les clients Mail ne sont pas tous au point pour l'UTF-8

			$sujet="";
			$corps="";

			$emailcontact = new Variable("emailcontact");
			$emailfrom = new Variable("emailfrom");
			$nomsite = new Variable("nomsite");
			$urlsite = new Variable("urlsite");

			/* Message client */
			$msg = new Message("mailconfirmcli");

			$msgdesc = new Messagedesc($msg->id,$commande->lang);
			$sujet = $this->substitmail($msgdesc->titre, $commande);

			$corps = $msgdesc->description;
			$corps = $this->substitmail($corps, $commande);
			$corpstext = $msgdesc->descriptiontext;
			$corpstext = $this->substitmail($corpstext,$commande);

			/* Message admin */
        	$msg->charger("mailconfirmadm");
        	$msgdesc = new Messagedesc($msg->id);
			$corps2 = $msgdesc->description;

			$sujet2 = $this->substitmail($msgdesc->titre, $commande);
			$corps2 = $this->substitmail($corps2, $commande);
			$corpstext2 = $msgdesc->descriptiontext;
			$corpstext2 = $this->substitmail($corpstext2,$commande);

			$client = new Client($commande->client);

			$smtp = new Smtpconfig(1);

			//envoi du mail au client
			$mailclient = new Mail();

			if($smtp->active){
				$mailclient->IsSMTP();
				$mailclient->Host = $smtp->serveur;
				$mailclient->Port = ($smtp->port!="")?$smtp->port:25;
				if($smtp->username != ""){
					$mailclient->SMTPAuth = true;
					$mailclient->Username = $smtp->username;
					$mailclient->Password = $smtp->password;

					$mailclient->SMTPSecure = ($smtp->secure != "")?$smtp->secure:"";
				}
			}
			else{
				$mailclient->IsMail();
			}
			$mailclient->FromName = utf8_decode($nomsite->valeur);
			$mailclient->From = utf8_decode($emailfrom->valeur);
			$mailclient->Subject = utf8_decode($sujet);
			$mailclient->MsgHTML(utf8_decode($corps));
			$mailclient->AltBody = utf8_decode($corpstext);
			$mailclient->AddAddress($client->email, utf8_decode($client->prenom . " " . $client->nom));

			$mailclient->send();

			//envoi du mail a l'admin
			$mail = new Mail();

			if($smtp->active){
				$mail->IsSMTP();
				$mail->Host = $smtp->serveur;
				$mail->Port = ($smtp->port!="")?$smtp->port:25;
				if($smtp->username != ""){
					$mail->SMTPAuth = true;
					$mail->Username = $smtp->username;
					$mail->Password = $smtp->password;

					$mail->SMTPSecure = ($smtp->secure != "")?$smtp->secure:"";
				}
			}
			else{
				$mail->IsMail();
			}
			$mail->FromName = utf8_decode($nomsite->valeur);
			$mail->From = utf8_decode($emailfrom->valeur);
			$mail->CharSet = "utf-8";
			$mail->Subject = utf8_decode($sujet2);
			$mail->MsgHTML(utf8_decode($corps2));
			$mail->AltBody = utf8_decode($corpstext2);
			$mail->AddAddress($emailcontact->valeur, utf8_decode($nomsite->valeur));

			$mail->send();

		}

		function substitmail($corps, $commande ){

			$emailcontact = new Variable("emailcontact");
			$emailfrom = new Variable("emailfrom");
			$nomsite = new Variable("nomsite");
			$urlsite = new Variable("urlsite");

		  	$jour = substr($commande->date, 8, 2);
  			$mois = substr($commande->date, 5, 2);
  			$annee = substr($commande->date, 0, 4);

  			$heure = substr($commande->date, 11, 2);
  			$minute = substr($commande->date, 14, 2);
  			$seconde = substr($commande->date, 17, 2);

			$client = new Client($commande->client);

			$paiement = new Modules($commande->paiement);
			$paiementdesc = new Modulesdesc($paiement->nom, $commande->lang);

			$transport = new Modules($commande->transport);
			$transportdesc = new Modulesdesc($transport->nom, $commande->lang);


			$total = $commande->total();
			$totcmdport = $commande->port + $total;

			$adresse = new Venteadr($commande->adrlivr);

			$raisondesc = new Raisondesc();
			$raisondesc->charger($adresse->raison, $commande->lang);

			$nom = $adresse->nom;
			$prenom = $adresse->prenom;
			$entreprise = $adresse->entreprise;
			$adresse1 = $adresse->adresse1;
			$adresse2 = $adresse->adresse2;
			$adresse3 =  $adresse->adresse3;
			$cpostal =  $adresse->cpostal;
			$ville = $adresse->ville;
			$pays = new Paysdesc($adresse->pays,$commande->lang);

			$corps = str_replace("__COMMANDE_REF__", $commande->ref, $corps);
			$corps = str_replace("__COMMANDE_DATE__", $jour . "/" . $mois . "/" . $annee, $corps);
			$corps = str_replace("__COMMANDE_HEURE__", $heure . ":" . $minute, $corps);
			$corps = str_replace("__COMMANDE_TRANSACTION__", $commande->transaction, $corps);
			$corps = str_replace("__COMMANDE_PAIEMENT__", $paiementdesc->titre, $corps);
			$corps = str_replace("__COMMANDE_TOTALPORT__", $totcmdport-$commande->remise, $corps);
			$corps = str_replace("__COMMANDE_TOTAL__", $total, $corps);
			$corps = str_replace("__COMMANDE_PORT__", $commande->port, $corps);
			$corps = str_replace("__COMMANDE_REMISE__", $commande->remise, $corps);
			$corps = str_replace("__COMMANDE_TRANSPORT__", $transportdesc->titre, $corps);
			$corps = str_replace("__COMMANDE_TRANSPORTCHAPO__", $transportdesc->chapo, $corps);
			$corps = str_replace("__COMMANDE_LIVRRAISON__", $raisondesc->court, $corps);
			$corps = str_replace("__COMMANDE_LIVRNOM__",$nom, $corps);
			$corps = str_replace("__COMMANDE_LIVRPRENOM__", $prenom, $corps);
			$corps = str_replace("__COMMANDE_LIVRENTREPRISE__",$entreprise, $corps);
			$corps = str_replace("__COMMANDE_LIVRADRESSE1__", $adresse1, $corps);
			$corps = str_replace("__COMMANDE_LIVRADRESSE2__", $adresse2, $corps);
			$corps = str_replace("__COMMANDE_LIVRADRESSE3__", $adresse3, $corps);
			$corps = str_replace("__COMMANDE_LIVRCPOSTAL__", $cpostal, $corps);
			$corps = str_replace("__COMMANDE_LIVRVILLE__", $ville, $corps);
			$corps = str_replace("__COMMANDE_LIVRPAYS__", $pays->titre, $corps);
			$corps = str_replace("__COMMANDE_LIVRTEL__", $adresse->tel, $corps);

			$corps = str_replace("__NOMSITE__", $nomsite->valeur, $corps);
			$corps = str_replace("__URLSITE__", $urlsite->valeur, $corps);


			$adresse = new Venteadr($commande->adrfact);

			$raisondesc = new Raisondesc();
			$raisondesc->charger($adresse->raison);

			$pays = new Paysdesc();
			$pays->charger($adresse->pays);

			$corps = str_replace("__CLIENT_REF__", $client->ref, $corps);
			$corps = str_replace("__CLIENT_RAISON__",$raisondesc->court, $corps);
			$corps = str_replace("__CLIENT_ENTREPRISE__", $client->entreprise, $corps);
			$corps = str_replace("__CLIENT_SIRET__", $client->siret, $corps);
			$corps = str_replace("__CLIENT_FACTNOM__", $adresse->nom, $corps);
			$corps = str_replace("__CLIENT_FACTPRENOM__", $adresse->prenom, $corps);
			$corps = str_replace("__CLIENT_ADRESSE1__", $adresse->adresse1, $corps);
			$corps = str_replace("__CLIENT_ADRESSE2__", $adresse->adresse2, $corps);
			$corps = str_replace("__CLIENT_ADRESSE3__", $adresse->adresse3, $corps);
			$corps = str_replace("__CLIENT_CPOSTAL__", $adresse->cpostal, $corps);
			$corps = str_replace("__CLIENT_VILLE__", $adresse->ville, $corps);
			$corps = str_replace("__CLIENT_PAYS__", $pays->titre, $corps);
			$corps = str_replace("__CLIENT_EMAIL__", $client->email, $corps);
			$corps = str_replace("__CLIENT_TELFIXE__", $client->telfixe, $corps);
			$corps = str_replace("__CLIENT_TELPORT__", $client->telport, $corps);

            $pattern = '{<VENTEPROD>((?:(?:(?!<VENTEPROD[^>]*>|</VENTEPROD>).)++|<VENTEPROD[^>]*>(?1)</VENTEPROD>)*)</VENTEPROD>}si';
            preg_match($pattern, $corps, $cut);

			$corps = str_replace("<VENTEPROD>", "", $corps);
			$corps = str_replace("</VENTEPROD>", "", $corps);

			$res="";


			$venteprod = new Venteprod();

			$query = "select * from $venteprod->table where commande=\"" . $commande->id . "\"";
			$resul = mysql_query($query, $venteprod->link);


			while($row = mysql_fetch_object($resul)){

				$temp = str_replace("__VENTEPROD_TITRE__", $row->titre, $cut[1]);
                $temp =  str_replace("__VENTEPROD_REF__", $row->ref, $temp);
                $temp =  str_replace("__VENTEPROD_CHAPO__", $row->chapo, $temp);
				$temp =  str_replace("__VENTEPROD_QUANTITE__", $row->quantite, $temp);
				$temp =  str_replace("__VENTEPROD_PRIXU__", $row->prixu, $temp);
				$temp =  str_replace("__VENTEPROD_TOTAL__", $row->prixu * $row->quantite, $temp);
				$res .= $temp;

			}
			$corps = str_replace($cut[1], $res, $corps);

			return $corps;

		}

	}
?>